<template>
	<view class="content" catchtouchmove="true">
			<scroll-view class="tab " id="tab" :show-scrollbar="false" :scroll-x="true" :scroll-into-view="tabScrollInto">
				<view class="tabContent" style="display: flex; flex-direction: column;">
					<view style="display: flex;justify-content: space-around;">
						<view class="tabItem" v-for="(tab,tabItemIndex) in tabList" :key="tab.id" :id="tab.id" :data-id="tabItemIndex"
							:data-current="tabItemIndex" @click="pressScrollViewItem">
							<text class="tabItemTitle"
								:class="tabIndex==tabItemIndex ? 'tabItemTitleActive' : ''"
								:style="{width:tabList[tabItemIndex].width + 'px'}"
								>{{tab.title}}</text>
						</view>
					</view>
					<view class="tabLineView">
						<view class="tabLine" :class="isPress ? 'tabLineActive':'tabLineActive'"
							:style="{left: tabItemLineLeft + 'px', width: tabItemLineWidth + 'px'}"></view>
					</view>
				</view>
			</scroll-view>
			<navigator url="./new">
				<view class="u-text-center add-container">
					+ 新增
				</view>
			</navigator>
			
		
		<swiper class="childPageView" :current="tabIndex" :duration="250" @change="swiperChange"
			@animationfinish="swiperChangeEnd" @onAnimationEnd="swiperChangeEnd" :style="{height: swiperHeight+'px'}">

			<swiper-item class="childPageViewItem" v-for="(page, childPageViewItemIndex) in tabList" :key="childPageViewItemIndex">
				<!-- 未读 -->
				<scroll-view v-if="childPageViewItemIndex == 0" scroll-y="true" :style="{height: swiperHeight+'px'}">
					<view class="">
						<view class="news-container" v-for="(data,itemIndex) in item1Data" :key="itemIndex">
							<view class="" style="font-size: 15px; font-weight: 700;">
								{{data.sender}}
							</view>
							<view class="u-line-2" style="font-size: 14px;">
								{{data.title}}
							</view>
						</view>
					</view>
				</scroll-view>

				<!-- 已读 -->
				<scroll-view v-if="childPageViewItemIndex != 0" scroll-y="true" :style="{height: swiperHeight+'px'}">
					<view class="">
						<view class="news-container" v-for="(data,itemIndex) in item2Data" :key="itemIndex">
							{{data.title}}
						</view>
					</view>
				</scroll-view>

			</swiper-item>
		</swiper>




	</view>
</template>

<script>
	// import cell1 from './cell1.vue';

	export default {
		components: {
			// cell1
		},
		data() {
			return {
				tabList: [{
					id: "tabItem0",
					title: '未读',
					width:100,
				}, {
					id: "tabItem1",
					title: '已读',
					width:100,
				}],
				tabIndex: 0,
				tabScrollInto: "",
				tabListSize: {},
				tabItemLineLeft: 0,
				tabItemLineWidth: 0,
				isPress: false,
				swiperHeight: 0,
				//未读信息
				item1Data: [{
					sender:'老板',
					title: 'uView组件功能的实现，并不依赖全局样式，内置的一些类名，只是提供一些基础且常用的样式，仅此而已。',
				}, {
					title: '8楼',
				}, 
				{
					title: '8楼',
				},
				{
					title: '8楼',
				},
				{
					title: '8楼',
				},
				{
					title: '8楼',
				},
				{
					title: '8楼',
				},
				{
					title: '8楼',
				},
				{
					title: '8楼',
				},
				{
					title: '8楼',
				},{
					title: '9楼',
				}],
				//已读信息
				item2Data: [{
					title: '1楼',
				}, {
					title: '2楼',
				}, {
					title: '3楼',
				}, {
					title: '4楼',
				}, {
					title: '5楼',
				}, {
					title: '6楼',
				}, {
					title: '7楼',
				}, {
					title: '8楼',
				}, {
					title: '9楼',
				}],
				

			}
		},
		onLoad() {

			let system = uni.getSystemInfoSync();
			this.swiperHeight = system.windowHeight - 44;
			console.log(this.swiperHeight);
		},
		onReady() {

			this.setTabItemDistance();
			this.setTabSelect(this.tabIndex);
		},
		methods: {
			pressScrollViewItem(e) {
				let index = e.target.dataset.current || e.currentTarget.dataset.current;
				this.isPress = true;
				this.setTabSelect(index);
			},
			swiperChange(e) {
				let index = e.target.current || e.detail.current;

				this.tabIndex = index;
				this.setTabSelect(this.tabIndex);
			},
			setTabItemDistance() {

				var queryTabSize = uni.createSelectorQuery().in(this);
				for (var i = 0; i < this.tabList.length; i++) {
					queryTabSize.select('#' + this.tabList[i].id).boundingClientRect();
				}
				queryTabSize.exec(rects => {
					rects.forEach((rect) => {
						this.tabListSize[rect.dataset.id] = rect;
					})
					console.log(this.tabListSize);
					this.setTabItemLinePosition(this.tabListSize[this.tabIndex].left, this.tabListSize[this
							.tabIndex]
						.width);
				});

			},
			setTabItemLinePosition(left, width) {
				this.tabItemLineLeft = left;
				this.tabItemLineWidth = width;
			},
			swiperChangeEnd(e) {

				this.setTabItemLinePosition(this.tabListSize[this.tabIndex].left, this.tabListSize[this.tabIndex].width);

			},
			setTabSelect(index) {

				this.tabIndex = index;
				this.tabScrollInto = this.tabList[index].id;
				console.log(this.tabScrollInto);

			},

		}
	}
</script>

<style lang="scss">
	.content {
		/* flex: 1; */
		display: flex;
		flex-direction: column;
		overflow: hidden;
		background-color: #ffffff;
	}
	.add-container{
		margin:5px 0;
		width: 100%;
		height: 40px;
		line-height: 40px;
		font-size: 16px;
		background-color: $u-type-primary;
		color: $u-type-info-light;
	}
	.news-container{
		width: 90%; 
		height: 150rpx; 
		margin: 0 auto 10rpx; 
		padding: 5px 10px;
		background-color: #ffffff;
		border-bottom: 1px solid #dcdcdc;
	}
	.tab {
		/* #ifdef APP-PLUS */
		width: 100vw;
		/* #endif */
		height: 44px;
		display: flex;
		flex-direction: row;
		/* #ifndef APP-PLUS */
		white-space: nowrap;
		/* #endif */
	}

	/* 隐藏滚动条 */

	.tab ::-webkit-scrollbar {
		display: none;
		width: 0 !important;
		height: 0 !important;
		-webkit-appearance: none;
		background: transparent;
	}

	.tabLineView {
		position: relative;
		height: 2px;
		background-color: transparent;
		
	}

	.tabLine {
		position: absolute;
		top: 0;
		bottom: 0;
		width: 0;
		background-color: $u-type-primary;
	}

	.tabLineActive {
		transition-duration: 0.1s;
		transition-property: left;
	}

	.childPageView {
		display: flex;
		flex-direction: column;
	}

	.tabItem {
		/* #ifndef APP-PLUS */
		/* display: inline-block; */
		/* #endif */
		display: flex;
		
		
		/* flex-wrap: nowrap; */
		
		/* 
		padding-left: 20px;
		padding-right: 20px; */
		
	}

	.tabItemTitle {
		color: #333333;
		font-size: 15px;
		height: 42px;
		line-height: 40px;
		display: flex;
		flex-wrap: nowrap;
		align-items: center;
		justify-content: center;
	}

	.tabItemTitleActive {
		color: $u-type-primary;
	}

	.childPageViewItem {
		flex: 1;
		flex-direction: column;
	}
</style>
